package com.miyake.loveviewweb.server.controller;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.miyake.loveviewweb.server.meta.LvBlobMeta;
import com.miyake.loveviewweb.shared.model.LvBlob;

public class StaticChartController extends Controller {

    @Override
    public Navigation run() throws Exception {
        if (this.isGet()) {
            Key chartKey = Datastore.stringToKey(this.request.getParameter("key"));
            
            LvBlobMeta e = LvBlobMeta.get();
            List<Key> keyList = Datastore.query(e).filter(e.chartKey.equal(chartKey)).asKeyList();

            LvBlob blob = Datastore.get(LvBlob.class, keyList.get(0));
            byte[] b = blob.getByteData();
            
           // this.response.setHeader("Cache-Control", "no-cache");
            this.response.setContentType(blob.getContentType());
            this.response.getOutputStream().write(b ,0, b.length);
            this.response.getOutputStream().flush();
            this.response.getOutputStream().close();
             
            Datastore.delete(keyList);    
        }
        return null;//forward("staticChart.jsp");
    }
}
